import { getDataPlnel, getTable, deleteFile, downloadFile } from '@/api/fileManage'
import { Message } from 'element-ui'

const oldState = { // 用于重置state数据
  tableData: [
    { id: 1, name: '刘有清文档', size: '14.5M', people: '刘有清', email: '1964550834@qq.com', time: '2020-09-21 09:23:09' },
    { id: 2, name: '刘有清文档', size: '14.5M', people: '刘有清', email: '1964550834@qq.com', time: '2020-09-21 09:23:09' },
    { id: 3, name: '刘有清文档', size: '14.5M', people: '刘有清', email: '1964550834@qq.com', time: '2020-09-21 09:23:09' },
    { id: 4, name: '刘有清文档', size: '14.5M', people: '刘有清', email: '1964550834@qq.com', time: '2020-09-21 09:23:09' },
    { id: 5, name: '刘有清文档', size: '14.5M', people: '刘有清', email: '1964550834@qq.com', time: '2020-09-21 09:23:09' },
    { id: 6, name: '刘有清文档', size: '14.5M', people: '刘有清', email: '1964550834@qq.com', time: '2020-09-21 09:23:09' },
    { id: 7, name: '刘有清文档', size: '14.5M', people: '刘有清', email: '1964550834@qq.com', time: '2020-09-21 09:23:09' },
    { id: 8, name: '刘有清文档', size: '14.5M', people: '刘有清', email: '1964550834@qq.com', time: '2020-09-21 09:23:09' }

  ],
  panelData: [
    { id: 0, number: 123456, type: 'today_b', desc: '今日新增' },
    { id: 1, number: 123456, type: 'mouthAdd_o', desc: '本月新增' },
    { id: 2, number: 123456, type: 'user', desc: '累计用户' }

  ],
  params: {
    page: 0,
    size: 10,
    fileName: null,
    fileTime: null,
    userPhone: null
  },
  total: 0
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_tableData (state, data) {
    state.tableData = data
  },
  set_panelData (state, data) {
    state.panelData[0].number = data.todayCount
    state.panelData[1].number = data.monthCount
    state.panelData[2].number = data.total
  },
  set_page (state, data) {
    state.params.page = data - 1
  },
  set_size (state, data) {
    state.params.size = data
  },
  set_fileName (state, data) {
    state.params.fileName = data
  },
  set_userPhone (state, data) {
    state.params.userPhone = data
  },
  set_fileTime (state, data) {
    state.params.fileTime = data
  },
  set_total (state, data) {
    state.total = data
  }

}
const actions = {
  getDataPlnelData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getDataPlnel(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_panelData', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  getTableData ({ state, commit }) {
    return new Promise((resolve, reject) => {
      getTable(state.params).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data.list)
          commit('set_total', res.data.data.size)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
          commit('set_total', 0)
        } else {
          reject(res.data.message)
        }
      })
    })
  },

  deleteFileData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      deleteFile(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  downloadFiles ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      downloadFile(playload).then(res => {
        // console.log(res)
        // console.log(1)
        // resolve(res)
        // if (res.data.code === 200 && res.data) {

        // } else {

        // }
        if (res.data.type === 'application/json') {
          const reader = new FileReader()
          reader.readAsText(res.data, 'utf-8')
          reader.onload = () => {
            const newRes = JSON.parse(reader.result)
            // console.log(newRes)
            Message({
              type: 'error',
              message: newRes.message,
              duration: 1000
            })
          }
        } else {
          // 正确回调函数
          resolve(res)
        }
      })
    })
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
